﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using ScreenMonitor.Common;
using ScreenMonitor.ServerLib;
using ServerUI.Setting;

namespace ServerUI
{
    public partial class ScreenViewer : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public ScreenViewer()
        {
            InitializeComponent();
        }

        private ScreenViewerFull Viewer;
        private bool IsResize = false;

        public ScreenViewerMini ViewerMini { get; private set; }

        public ScreenViewer(ScreenViewerMini mini)
            : this()
        {
            this.ViewerMini = mini;
            Viewer = new ScreenViewerFull(mini);
        }

        public Image Img
        {
            get
            {
                return pictureBox1.Image;
            }
            set
            {
                pictureBox1.Image = value;
                Viewer.img = value;
            }
        }

        public string InfoTxt
        {
            get
            {
                return toolcname.Text;
            }
            set
            {
                toolcname.Text = value;
                this.Text = "监控-" + value;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ClientCommandCenter.PauseSendBmp(ClientManager.GetAllClient(ClientState.Connected));
            this.ViewerMini.Client.IsPausedSendImg = false;
            Viewer.IsControlMode = false;
            Viewer.TopMost = true;
            Viewer.WindowState = FormWindowState.Maximized;
            Viewer.ShowDialog();
            ViewerMini.Client.SendImgSize = new System.Drawing.Size(128, 128);
            ClientCommandCenter.StartSendBmp(ClientManager.GetAllClient(ClientState.Connected));
        }
       
        private void ScreenViewer_Load(object sender, EventArgs e)
        {

        }

        private void btnStartWatch_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.IsPausedSendImg = false;
        }

        private void btnPauseWatch_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.IsPausedSendImg = true;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.RefreshBitMap();
        }

        private void tooliset1_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgRate = 1000;
        }

        private void tooliset2_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgRate = 2000;
        }

        private void tooliset5_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgRate = 5000;
        }

        private void toolisetok_Click(object sender, EventArgs e)
        {
            try
            {
                ViewerMini.Client.SendImgRate = int.Parse(tooltsett.Text);
            }
            catch (Exception ex)
            {
#if DEBUG
                throw;
#else
                MessageBox.Show(ex.Message);
#endif
            }
        }

        private void toolset1616_Click(object sender, EventArgs e)
        {
            ToolStripDropDownItem item = (ToolStripDropDownItem)sender;
            ViewerMini.Client.SendImgSize = ToolClass.SplitSizeString(item.Text);
            timer1.Stop();
        }

        private void toolsetbmpsizeok_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgSize = ToolClass.SplitSizeString(toolsetsizecus.Text);
            timer1.Stop();
        }

        private void ScreenViewer_Resize(object sender, EventArgs e)
        {
            IsResize = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!IsResize)
                return;
            string t = this.panel1.Size.Width.ToString() + "*" + this.panel1.Size.Height.ToString();
            ViewerMini.Client.SendImgSize = this.panel1.Size;
            IsResize = false;
        }

        private void 自动调整ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void toolsetimageformat_Click(object sender, EventArgs e)
        {
            ToolStripDropDownItem item = (ToolStripDropDownItem)sender;
            this.ViewerMini.Client.SendImgFormat = item.Text.ToLower();
        }

        private void toolsConStart_Click(object sender, EventArgs e)
        {
            ClientCommandCenter.PauseSendBmp(ClientManager.GetAllClient(ClientState.Connected));
            Viewer.IsControlMode = true;
            Viewer.TopMost = true;
            Viewer.WindowState = FormWindowState.Maximized;
            Viewer.TCmdSend.Interval = ServerUIRateSetting.ControlRate;
            Viewer.TCmdSend.Start();
            ViewerMini.Client.StartControl();
            Viewer.ShowDialog();
            ViewerMini.Client.SendImgSize = new System.Drawing.Size(128, 128);
            Viewer.TCmdSend.Stop();
            ViewerMini.Client.StopControl();
            ClientCommandCenter.StartSendBmp(ClientManager.GetAllClient(ClientState.Connected));
        }

        private void toolsConT1_Click(object sender, EventArgs e)
        {
            ServerUIRateSetting.ControlRate = 1000;
        }

        private void toolsConT05_Click(object sender, EventArgs e)
        {
            ServerUIRateSetting.ControlRate = 500;
        }

        private void toolsConTOK_Click(object sender, EventArgs e)
        {
            try
            {
                ServerUIRateSetting.ControlRate = int.Parse(this.toolsConTCus.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void toolStripDropDownButton1_Click(object sender, EventArgs e)
        {
            toolsConTCus.Text = ServerUIRateSetting.ControlRate.ToString();
        }

        private void ScreenViewer_Deactivate(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgRate = 1000;
        }

        private void toolsImgSizeModeNormal_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgSizeMode = PictureBoxSizeMode.Normal;
        }

        private void toolsImgSizeModeStretch_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgSizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void toolsImgSizeModeZoom_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgSizeMode = PictureBoxSizeMode.Zoom;
        }

        private void toolsImgSizeModeCenter_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgSizeMode = PictureBoxSizeMode.CenterImage;
        }

        private void toolsImgSizeModeAutoSize_Click(object sender, EventArgs e)
        {
            ViewerMini.Client.SendImgSizeMode = PictureBoxSizeMode.AutoSize;
        }


    }
}
